package one.SnackGame;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;

public class SnackHead extends GameObj {
    //方向 up down left right
    private String direction = "right";

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    public SnackHead(Image image, int x, int y, GameFrame gf) {
        super(image, x, y, gf);
        //键盘操纵
        this.gf.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                changeDirection(e);
            }
        });
    }

    //控制操纵方向  WSAD
    public void changeDirection(KeyEvent e) {
        switch (e.getKeyCode()) {
            case KeyEvent.VK_A:
                if (!"right".equals(direction)) {
                    direction = "left";
                }
                break;
            case KeyEvent.VK_D:
                if (!"left".equals(direction)) {
                    direction = "right";
                }
                break;
            case KeyEvent.VK_W:
                if (!"down".equals(direction)) {
                    direction = "up";
                }
                break;
            case KeyEvent.VK_S:
                if (!"up".equals(direction)) {
                    direction = "down";
                }
                break;
            default:
                break;
        }
    }

    //蛇的移动
    public void move() {
        //蛇身移动
        java.util.List<SnackBody> snackBodies = this.gf.snackBodies;
        for (int i = snackBodies.size() - 1; i >= 1; i--) {
            snackBodies.get(i).x = snackBodies.get(i - 1).x;
            snackBodies.get(i).y = snackBodies.get(i - 1).y;
            //蛇头和蛇身相撞
            if (this.x == snackBodies.get(i).x && this.y == snackBodies.get(i).y) {
                //失败
                GameFrame.state = 3;
            }
        }
        snackBodies.get(0).x = this.x;
        snackBodies.get(0).y = this.y;
        //蛇头的移动
        switch (direction) {
            case "up":
                y -= 30;
                break;
            case "down":
                y += 30;
                break;
            case "left":
                x -= 30;
                break;
            case "right":
                x += 30;
                break;
        }
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        //蛇吃掉食物
        SnackFood food = this.gf.snackFood;
        //蛇身最后一节的坐标
        Integer newX = null;
        Integer newY = null;
        if (this.x == food.x && this.y == food.y) {
            //食物重新生成
            this.gf.snackFood = food.getFood();
            if (this.x == food.x && this.y == food.y){
                this.gf.snackFood = food.getFood();
                //获取蛇身最后一节
                SnackBody lastBody = this.gf.snackBodies.get(this.gf.snackBodies.size()-1);
                newX = lastBody.x;
                newY = lastBody.y;
                //分数加一
                this.gf.score++;
            }
            //通关判断
            if (this.gf.score == 15){
                //通关
                GameFrame.state = 4;
            }
        }
            move();
        if (newX != null && newY != null){
            this.gf.snackBodies.add(new SnackBody(GameUtility.bodyImage, newX, newY, this.gf));
        }
            //越界处理
            if (x < 0) {
                x = 570;
            } else if (x > 570) {
                x = 0;
            }
            if (y < 30) {
                y = 570;
            } else if (y > 570) {
                y = 30;
            }
        }
    }
